//
//  GCSettingViewController.m
//  GoldChampion
//
//  Created by Mac on 2018/4/11.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "GCSettingViewController.h"
#import "GCBindingPhoneViewController.h"
#import "GCSuggestViewController.h"
#import "GCAboutUsViewController.h"
#import "GCNotifacationViewController.h"
#import "GCPrivacySettingViewController.h"
@interface GCSettingViewController ()
@property(nonatomic, strong) NSArray                     *dataSouce;
@property(nonatomic, strong) UISwitch                     *productSwitch;
@property(nonatomic, strong) UISwitch                     *saveSwitch;
@end

@implementation GCSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self configTableFooterView];
    [self configDataSouce];

    // Do any additional setup after loading the view.
}

- (void)configDataSouce{
    __weak typeof(self) weakSelf =  self;
    STButton * bindingButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)
                                                     title:@"绑定"
                                                titleColor:TM_redColor
                                                 titleFont:16
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    bindingButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [bindingButton st_setBorderWith:1 borderColor:TM_redColor cornerRadius:0];
    [bindingButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedBingButton];
    }];
   STTableEasyModel * phoneModel =  [[STTableEasyModel alloc] initWithTextString:@"绑定手机号"
                                    imageName:@"-手机绑定"];
    //判断有无手机
//    if (!0) {
//        //没绑定手机
//        [bindingButton st_setBorderWith:1 borderColor:[UIColor clearColor] cornerRadius:0];
//        [bindingButton setTitleColor:SecendTextColor forState:UIControlStateNormal];
//    }
    phoneModel.accessoryView = bindingButton;
    
    
    STTableEasyModel * productModel =  [[STTableEasyModel alloc] initWithTextString:@"账号保护"
                                                                   imageName:@"保护"];
    self.productSwitch = [self deflutSwitch];
    productModel.accessoryView = self.productSwitch;
    
    
    STTableEasyModel * saveModel =  [[STTableEasyModel alloc] initWithTextString:@"自动保存作品到本地影集"
                                                                          imageName:@"保护"];
    self.saveSwitch = [self deflutSwitch];
    saveModel.accessoryView = self.saveSwitch;
    
    NSArray * section0 =  @[
                            phoneModel,
                            productModel,
                            [[STTableEasyModel alloc] initWithTextString:@"隐私设置"
                                                               imageName:@"私密专线"],
                            [[STTableEasyModel alloc] initWithTextString:@"通知设置"
                                                               imageName:@"通知方式设置"],
                            ];
    
    NSArray * section1 =  @[
                            [[STTableEasyModel alloc] initWithTextString:@"清除缓存"
                                                               imageName:@"删除99"],
                            saveModel
                            ];
    NSArray * section2 =  @[
                            [[STTableEasyModel alloc] initWithTextString:@"意见反馈"
                                                               imageName:@"意见反馈hui"],
                            [[STTableEasyModel alloc] initWithTextString:@"关于金冠"
                                                               imageName:@"guanyu"],
                            ];
    self.dataSouce = @[
                        section0,
                        section1,
                        section2
                        ];
    [self.tableView reloadData];
    
}
- (UISwitch*)deflutSwitch{
    UISwitch * switchView = [UISwitch new];
    switchView.onTintColor = UIColorFromRGBA(0x3ECD74);
    [switchView addTarget:self action:@selector(switchViewChangedValue:) forControlEvents:UIControlEventValueChanged];
    return switchView;
}
- (void)configTableFooterView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth , 44)
                                                        title:@"退出登录"
                                                   titleColor:SecendTextColor
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:[UIColor whiteColor]
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedLogoutButton];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.dataSouce[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 55;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    STTableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    [cell setSt_tableEasyModel:model];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    if ([model.textString containsString:@"意见反馈"]) {
        [self.navigationController pushViewController:[GCSuggestViewController new] animated:YES];
    }
    if ([model.textString containsString:@"关于金冠"]) {
        [self.navigationController pushViewController:[GCAboutUsViewController new] animated:YES];
    }
    if ([model.textString containsString:@"清除缓存"]) {
        [SVProgressHUD showSuccessWithStatus:@"清除成功"];
    }
    if ([model.textString containsString:@"通知设置"]) {
        [self.navigationController pushViewController:[GCNotifacationViewController new] animated:YES];
    }
    if ([model.textString containsString:@"隐私设置"]) {
        [self.navigationController pushViewController:[GCPrivacySettingViewController new] animated:YES];
    }
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
- (void)onSlectedBingButton{
    [self.navigationController pushViewController:[GCBindingPhoneViewController new] animated:YES];
}
- (void)switchViewChangedValue:(UISwitch*)sender{
    
}
- (void)onSelectedLogoutButton{
    [self showAlertCancelAndConfirm:@"确定要退出登录吗?" andWithBlock:^(NSString *name) {
        if ([name isEqualToString:@"确认"]) {
            [[STUserManger defult] removeUserPreferce];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }];
}
@end
